﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Text;

namespace WEALTH
{
    public partial class Customer : Page
    {
        int CurrentPage;//当前页数
        int PageSize;   //每页条数
        int PageCount;  //总页数
        int RecordCount;//总条数
        public string strLeftmenu = string.Empty;
        private ExcuteDao dao = new ExcuteDao();

        public string GetBuyDivStr(object isCanBuy, object listCount, object listId, object title)
        {
            bool isCB = (Boolean)isCanBuy;
            StringBuilder str = new StringBuilder();

            if (isCB)
            {
                str.Append("<tr><td colspan=2><table><tr>");
                str.Append("<td>需要积分：<strong class=\"tb-rmb-num\">" + listCount.ToString() + "</strong></td>");
                str.Append("<td>&nbsp;&nbsp;&nbsp;<a href=SendOrder.aspx?listviewid=" + listId.ToString() + "&point=" + listCount.ToString() + "&txTitle=" + title.ToString() + "><img src=\"images/buy.jpg\"></a></td>");


                //str.Append("<td><a href=\"ListView.aspx?listviewid="
                //        + listId.ToString() + "&menutype=" + string.IsNullOrEmpty(Request.QueryString["menutype"]) + "&isGift=1" + "\"><img src=images/buy.jpg /></a></td>");
                str.Append("</tr></table></td></tr>");
            }
            return str.ToString();
        }

        public string GetTitleStr(object title)
        {
            return AppFunction.GetString(title.ToString(), 60);
        }

        public string GetjianjieStr(object jianjie)
        {
            return AppFunction.GetString(jianjie.ToString(), 180);
        }

        public string GetImgUrl(object objtype)
        {
            string type = objtype.ToString().Trim();
            string url = "";
            if (type == "0")//html图标
            {
                url = @"../Images/ie.gif";
            }
            else if (type == "1")//word图标
            {
                url = @"../Images/msword.gif";
            }
            else
            {
                url = @"../Images/msexcel.gif";
            }
            return url;
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            this.DataList1.DataBinding += new EventHandler(DataList1_DataBinding);
            // 在此处放置用户代码以初始化页面
            PageSize = 10;//每页10条记录

            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["menuid"]))
                {
                    CurrentPage = 0;//当前页习惯设为0
                    ViewState["PageIndex"] = 0;//页索引也设为0

                    DataTable dt = dao.Get_List(-1);
                    //if (dt != null && dt.Rows.Count == 1)
                    //{
                    //    Response.Redirect("ListView.aspx");
                    //    return;
                    //}

                    ViewState["PageData"] = dt;
                    //计算总共有多少记录
                    RecordCount = dt.Rows.Count;

                    //计算总共有多少页
                    if (RecordCount % PageSize == 0)
                    {
                        PageCount = RecordCount / PageSize;
                    }
                    else
                    {
                        PageCount = RecordCount / PageSize + 1;
                    }

                    //this.TotalLbl.Text = "总" + PageCount.ToString() + "页";//显示总页数
                    ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ，而视图状态 viewstate相当于某个页面的 session

                    this.DataListBind();//不可以放在初始化条件之前就绑定，那样的话，如果仅有一页的数据，“下一页”页仍然显示
                }
            }
        }

        void DataList1_DataBinding(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataListBind();
        }

        //将数据绑定到Datalist控件
        public void DataListBind()
        {
            try
            {
                AspNetPager1.PageSize = 10;
                //初始化分页数据源实例            
                PagedDataSource pds = new PagedDataSource();
                //设置总行数           
                AspNetPager1.RecordCount = ((DataTable)ViewState["PageData"]).DefaultView.Count;
                //设置分页的数据源          
                pds.DataSource = ((DataTable)ViewState["PageData"]).DefaultView;
                //设置当前页         
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //设置每页显示页数           
                pds.PageSize = AspNetPager1.PageSize;
                //启用分页         
                pds.AllowPaging = true;
                //设置GridView的数据源为分页数据源          
                DataList1.DataSource = pds;
                this.DataList1.DataBind();
                //this.PreviousLB.Enabled = true;
                //this.NextLB.Enabled = true;
                //if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时，下一页链接按钮不可用
                //if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时，上一页按钮不可用
                //this.CurrentLbl.Text = "当前第"+(CurrentPage + 1).ToString()+"页";//当前页数

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
        {
            CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引
            PageCount = (int)ViewState["PageCount"];//获得总页数

            string cmd = e.CommandName;

            //判断cmd，以判定翻页方向
            switch (cmd)
            {
                case "prev"://上一页
                    if (CurrentPage > 0) CurrentPage--;
                    break;

                case "next":
                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
                    break;

                case "first"://第一页
                    CurrentPage = 0;
                    break;

                case "end"://最后一页
                    CurrentPage = PageCount - 1;
                    break;

                //case "jump"://跳转到第几页
                //    if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                //    {
                //        return;
                //    }
                //    else
                //    {
                //        CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                //        break;
                //    }
            }
            ViewState["PageIndex"] = CurrentPage;//获得当前页

            this.DataListBind();//重新将DataList绑定到数据库
        }
        /// <summary>
        /// 设置左侧菜单
        /// </summary>
        private void SetLeftMenu()
        {
            string strmenutype = Request.QueryString["menutype"].ToString();

            DataTable dt = dao.Get_LeftMenuALL("", strmenutype);
            strLeftmenu = string.Empty;
            strLeftmenu += "<li id=\"one\" class=\"files\"><a href=\"#\">" + strmenutype + "</a><ul class=\"sub-menu\">";

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int intMenuID = Convert.ToInt32(dt.Rows[i]["MenuID"]);
                    string strmenuname = dt.Rows[i]["MenuName"].ToString();
                    DataTable menuDt = dao.Get_ListALL("", "", intMenuID, "");

                    if (menuDt.Rows.Count == 1)
                    {
                        int listid = Convert.ToInt32(menuDt.Rows[0]["listID"]);

                        strLeftmenu += "<li><a href=\"ListView.aspx?listviewid="
                        + listid + "&menutype=" + strmenutype + "\" title=\"\"><em>"
                        + (i + 1).ToString().PadLeft(2, '0') + "</em>" + strmenuname + "</a></li>";
                    }
                    else
                    {
                        strLeftmenu += "<li><a href=\"List.aspx?menuid="
                            + intMenuID + "&menutype=" + strmenutype + "\" title=\"\"><em>"
                            + (i + 1).ToString().PadLeft(2, '0') + "</em>" + strmenuname + "</a></li>";
                    }
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>企业文化</a></li>";
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>精英团队</a></li>";
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>公司新闻</a></li>";
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>合作伙伴</a></li>";
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>招贤纳士</a></li>";
                    //strLeftmenu += "<li><a href=\"#\" title=\"\"><em>01</em>联系我们</a></li>";
                }
            }
            strLeftmenu += "</ul></li>";
        }


    }
}